package com.dudu.common.util

import android.app.Activity
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.transition.Slide.GravityFlag
import com.dudu.common.R
import com.dudu.common.util.DensityUtils.dp
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar


/**
 * <pre>
 *     author : dzc
 *     time   : 2024/10/31
 *     desc   : 自定义SnackBar，替代Toast提示框, 必须由activity进行调用
 * </pre>
 */
object SnackBarUtils {

    @GravityFlag
    var gravity: Int = Gravity.TOP

    var duration: Int = Snackbar.LENGTH_LONG

    fun show(activity:Activity, msg: String){
        val contentViewGroup = activity.findViewById(android.R.id.content) as? ViewGroup
        var rootView = contentViewGroup?.getChildAt(0)
        rootView?:let {
            rootView = activity.window.decorView.rootView
        }

        rootView?.let {
            val snackBar = Snackbar.make(it, msg, duration)

            val params: FrameLayout.LayoutParams = FrameLayout.LayoutParams(
                snackBar.view.layoutParams.width,
                snackBar.view.layoutParams.height
            )
            params.gravity = gravity
            params.topMargin = 74.dp
            params.leftMargin = 100.dp
            params.rightMargin = 100.dp
            snackBar.view.setLayoutParams(params)
            snackBar.view.setPadding(15.dp, 30.dp, 15.dp, 30.dp)

            val tv = snackBar.view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
            tv.textSize = 40f
            tv.setTextColor(getResColor(R.color.white))
            tv.textAlignment = View.TEXT_ALIGNMENT_GRAVITY
            tv.gravity = Gravity.CENTER

            snackBar.view.setBackgroundResource(R.drawable.bg_toast_redius_black)
            snackBar.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_FADE)
            snackBar.show()
        }

    }

}